﻿/*Create By szl*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;

namespace MINERP.DAL.InnerReturn
{
    public class InnerReturnDAL : DALBase
    {
        /// <summary>
        /// 根据照片barcode取得照片信息
        /// </summary>
        /// <param name="inBarCode"></param>
        /// <returns></returns>
        public photoInfo GetPhotoByBarCode(string inBarCode)
        {
            return DataEntites.photoInfo.Where(p => p.PhotoBarCode == inBarCode).FirstOrDefault();
        }
        /// <summary>
        /// 取得产品 Flow
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public List<WorkOrderFlowList> GetFlowsByProduct(int inProductId)
        {
            return DataEntites.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == inProductId).ToList();
        }
        /// <summary>
        /// 获取所有的用户
        /// </summary>
        /// <returns></returns>
        public List<users> GetAllUsers()
        {
            return DataEntites.users.ToList();
        }
        /// <summary>
        /// 取得所有的节点信息
        /// </summary>
        /// <returns></returns>
        public List<tb_Tech> GetAllTechs()
        {

            int workorder = GetStartTech().WokerOrder;
            return DataEntites.tb_Tech.Where(p => p.WokerOrder > workorder).ToList();
        }
        /// <summary>
        /// 获取可用开始节点
        /// </summary>
        /// <returns></returns>
        public tb_Tech GetStartTech()
        {
            tb_Tech tech = DataEntites.tb_Tech.Where(p => p.IsDesign == true).FirstOrDefault();

            return tech;
        }
        public List<WorkOrderFlowList> GetFlows(int inProductId, int inEndTechID)
        {
            tb_Tech endTech = DataEntites.tb_Tech.Where(p => p.TechId == inEndTechID).FirstOrDefault();

            int startworkerOrder = GetStartTech().WokerOrder;
            return
                DataEntites.WorkOrderFlowList.Where(
                    p => p.WorkOrder > startworkerOrder && p.WorkOrder < endTech.WokerOrder && p.orderInfo.OrderProductId == inProductId).ToList().OrderBy(p => p.WorkOrder).ToList();

        }
        /// <summary>
        /// 取得产品
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public orderInfo GetProductbyId(int inProductId)
        {
            return DataEntites.orderInfo.Where(p => p.OrderProductId == inProductId).FirstOrDefault();
        }
        /// <summary>
        /// 
        /// 取得照片信息
        /// </summary>
        /// <param name="inPhotoId"></param>
        /// <returns></returns>
        public photoInfo GetPhotoById(int inPhotoId)
        {
            return DataEntites.photoInfo.Where(p => p.photoId == inPhotoId).FirstOrDefault();
        }
        /// <summary>
        /// 取得节点信息
        /// </summary>
        /// <param name="inTechId"></param>
        /// <returns></returns>
        public tb_Tech GetTechById(int inTechId)
        {
            return DataEntites.tb_Tech.Where(p => p.TechId == inTechId).FirstOrDefault();
        }

        public void SaveDate()
        {
            DataEntites.SaveChanges();
        }
        //public void insert(InnerReturnOrder innerReturnOrder)
        //{
        //    DataEntites.AddToInnerReturnOrder(innerReturnOrder);
        //}
        public bool IsFanDanIng(string inBarCode)
        {
            List<photoInfo> infos = DataEntites.photoInfo.Where(p => p.PhotoBarCode == inBarCode).ToList();
            if (infos.Count > 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public users GetUser(int inProductId, int inTechId)
        {
            WorkOrderFlowList workFlow =
                DataEntites.WorkOrderFlowList.Where(
                    p => p.orderInfo.OrderProductId == inProductId && p.tb_Tech.TechId == inTechId).FirstOrDefault();
            if (workFlow==null)
                return null;
            return DataEntites.users.Where(p => p.userID == workFlow.UserId).FirstOrDefault();
        }
        public users GetUser( int inTechId)
        {
            return DataEntites.users.Where(p => p.userID == inTechId).FirstOrDefault();
        }
        /// <summary>
        /// 取得节点
        /// </summary>
        /// <param name="inTechId"></param>
        /// <returns></returns>
        public List<users> GetUsersByTechId(int inTechId)
        {
            tb_Tech tech = DataEntites.tb_Tech.Where(p => p.TechId == inTechId).FirstOrDefault();
            tech.users.Load();
            List<users> userses = tech.users.ToList();
            return userses;
        }

        /// <summary>
        /// 获取反单记录
        /// </summary>
        /// <returns></returns>
        public System.Data.DataTable GetUnComplteFanDan()
        {
            string strSQL = "select   orderInfo.OrderProductId,OrderProductCode,OrderProductName,CtmName, " +
                            "TechName,ParentProductId,shopName,InnerReturnOrder.CreateTime,users.UserName " +
                            "from orderInfo, dbo.WorkOrderFlowList,dbo.tb_Tech,                           " +
                            "dbo.Tb_OrderBag,dbo.Shop,InnerReturnOrder,dbo.users                          " +
                            "where                                                                        " +
                            "orderInfo.OrderProductId=WorkOrderFlowList.OrderProductId                    " +
                            "and dbo.Tb_OrderBag.OrderBagId=orderInfo.OrderBagId                          " +
                            "and Tb_OrderBag.shopID=Shop.shopID                                           " +
                            "and tb_Tech.TechId=WorkOrderFlowList.TechId                                  " +
                            "and users.userID=dbo.InnerReturnOrder.CreateUid                              " +
                            "and ProductType=" +(int)OrderInfoType.InnerReturn+
                            "and orderInfo.status!=" + (int)OrderInfoStatus.Completed+
                            "and WorkOrderFlowList.Status=" + (int)OrderFlowListStatus.Useable +
                            "and InnerReturnOrder.childorderproductid=orderInfo.OrderProductId            " +
                            "order by orderInfo.orderproductid desc                                       "; ;

            return this.ExceSQL(strSQL);
        }

        public System.Data.DataTable GetPhotosByProductID(int inProductId)
        {
            string strSQL = "select  photoId,PhotoBarCode,photoName,photoInfo.Height,photoInfo.Width," +
                            "photoInfo.status,OrderProductCode,OrderProductName                      " +
                            "from  dbo.photoInfo,orderInfo                                           " +
                            "where  photoInfo.OrderProductId=orderInfo.OrderProductId                " +
                            "and orderInfo.OrderProductId= " + inProductId;

            return this.ExceSQL(strSQL);
        }

        public int GetProductIDByGridId(int inGridId)
        {
            return DataEntites.Grid.FirstOrDefault(p=>p.GridId==inGridId).SomeId;
        }
    }
}
